function [Xu,gu,nw,x1,x2] = empirical_density_generate_grid(nu,boo1,boo2,xx1,xx2,gr)
% This function generates the covariates and g for unique households, and
% count the number of unique households in each grid in two-dimensional space. 
% This is used for making consistent grid space across waves. 

% Inputs: 
% (1) nu: number of unique households 
% (2) boo1: lower bound for each bin on the x-axis 
% (3) boo2: lower bound for each bin on the y-axis
% (4) xx1: bin index for each data point on the x-axis 
% (5) xx2: bin index for each data point on the y-axis
% (6) gr: sorted g, according to X ordering

% Outputs:
% (1) Xu: Unique X values 
% (2) gu: sum of g for each unique X value
% (3) nw: number of individuals in each Xu 
% (4) x1: x dimension of Xu
% (5) x2: y dimension of Xu

%% 
Xu=nan(nu,2); gu=nan(nu,1); nw = nan(nu,1);
k=1;
for i = 1:length(boo1)
   for j = 1:length(boo2)
       bin_boo = logical((xx1==i).*(xx2==j));
       % identify individuals in each pair of covariate combination
       Xu(k,:)=[boo1(i),boo2(j)];
       gu(k,1) = sum(gr(bin_boo)); % net savings for each unique covariate combo
       nw(k,1) = sum(bin_boo); % count number of individuals in the pair of covariate combo
       k=k+1;
   end
end

x1 = Xu(:,1);
x2 = Xu(:,2);

end